home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / COLOR.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  6KB  |  242 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/color.c,v $
  3.  * Date:     $Date: 1992/09/09 00:09:50 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. /*     
  10. ** File: onewindow.c
  11. ** Generic program schema for a one window program    
  12. */ 
  13.     
  14. #include <stdio.h>    
  15. #include <X11/Xlib.h>    
  16. #include <X11/Xutil.h>    
  17.     
  18. /*    
  19. ** debug lists all events to an 
  20. ** xterm window as they occur
  21. */    
  22. #define DEBUG    
  23.     
  24. char WINDOW_NAME[] = "Window";    
  25. char ICON_NAME[] = "Icon";    
  26.     
  27. Display *display;  /* the display device */    
  28. int     screen;    /* the screen on the display */    
  29. Window  main_window;    
  30. GC    gc;    
  31. unsigned long  foreground, background;    
  32.  
  33. char RED[] = "Red";
  34. char BLUE[] = "Blue";
  35. XColor    RGB_color, hardware_color;
  36. Colormap    color_map;
  37.  
  38.     
  39. /*    
  40. ** 
  41. */    
  42.     
  43. Window    
  44. openWindow (x, y, width, height, border_width,
  45.             argc, argv)    
  46.     int x, y;  /* coords of the upper left 
  47.                   corner in pixels */
  48.     int width,    
  49.         height;  /* size of the window in pixels */    
  50.     int border_width;  /* the border width is 
  51.                           not included in the    
  52.                           other dimensions */    
  53.     int argc;    
  54.     char **argv;    
  55. {    
  56.     Window  new_window;    
  57.     XSizeHints  size_hints;    
  58.         
  59.     /* now create the window */    
  60.     new_window = XCreateSimpleWindow (display,     
  61.                         DefaultRootWindow (display),    
  62.                         x, y, width, height,    
  63.                         border_width,    
  64.                         foreground, background);    
  65.             
  66.     /* set up the size hints for the window manager */
  67.     size_hints.x = x;    
  68.     size_hints.y = y;    
  69.     size_hints.width = width;    
  70.     size_hints.height = height;    
  71.     /* and state what hints are included */    
  72.     size_hints.flags = PPosition | PSize;    
  73.     
  74.     /* let the window manager know about the window */
  75.     XSetStandardProperties (display, new_window,    
  76.           WINDOW_NAME, ICON_NAME,    
  77.           None,      /* no icon map */    
  78.           argv, argc, &size_hints);    
  79.     
  80.     /* Decide what events the window will receive */
  81.     XSelectInput (display, new_window,    
  82.           (ButtonPressMask | KeyPressMask | 
  83.            ExposureMask));    
  84.     
  85.     /* Return the window ID */    
  86.     return (new_window);    
  87. }    
  88.     
  89. /*    
  90. ** 
  91. */    
  92. GC
  93. getGC ()    
  94. {   GC gc;
  95.     XGCValues gcValues;    
  96.     
  97.     gc = XCreateGC (display, main_window,     
  98.                 (unsigned long) 0, &gcValues);    
  99.     
  100.     XSetBackground (display, gc, background);    
  101.     XSetForeground (display, gc, foreground);    
  102.  
  103.     return (gc);
  104. }    
  105.     
  106. /*    
  107. ** Terminate the program gracefully    
  108. */    
  109. quitX ()    
  110. {    
  111.     XCloseDisplay (display);    
  112.     exit (0);    
  113. }    
  114.     
  115.     
  116. /* 
  117. ** An expose event occurs when the contents of
  118. ** a window are invalidated and at least some
  119. ** of it needs to be redrawn 
  120. */ 
  121. void    
  122. doExposeEvent (pEvent)    
  123.     XExposeEvent *pEvent;    
  124. {    
  125.     XDrawImageString (display, main_window, gc,    
  126.           10, 10, "Press q or any button to quit",    
  127.           strlen ("Press q or any button to quit"));    
  128. }    
  129.     
  130. /* 
  131. ** A button has been pressed within the window - quit
  132. */ 
  133. void doButtonPressEvent (pEvent)    
  134.     XButtonEvent *pEvent;    
  135. {    
  136.     quitX ();    
  137. }    
  138.     
  139. /* 
  140. ** A key has been pressed. 
  141. ** It needs to be decoded and acted on.
  142. ** Quit if it is a 'q'.
  143. */ 
  144. void    
  145. doKeyPressEvent (pEvent)    
  146.     XKeyEvent *pEvent;    
  147. {   int key_buffer_size = 10;    
  148.     char key_buffer[9];    
  149.     XComposeStatus compose_status;    
  150.     KeySym key_sym;    
  151.     
  152.     XLookupString (pEvent, key_buffer, 
  153.           key_buffer_size,    
  154.           &key_sym, &compose_status);    
  155.     if (key_buffer[0] == 'q')    
  156.           quitX ();    
  157. }    
  158.     
  159. void
  160. initX ()    
  161. {    
  162.     int depth;
  163.  
  164.     /* set the display name from the environment
  165.        vbl DISPLAY */    
  166.     display = XOpenDisplay (NULL);    
  167.  
  168.     if  (display == NULL)    
  169.     {      fprintf (stderr, 
  170.                     "Unable to open display %s\n",    
  171.                     XDisplayName (NULL));    
  172.           exit (1);    
  173.     }    
  174.     screen = DefaultScreen (display);    
  175.  
  176.     /* Find the depth of the color map */    
  177.     depth = DefaultDepth (display, screen);    
  178.         
  179.     /* set the defaults in case we can't use color */
  180.     foreground = BlackPixel (display, screen);    
  181.     background = WhitePixel (display, screen);    
  182.     
  183.     if (depth > 1)
  184.     {   /* not monochrome */
  185.         color_map = DefaultColormap (display, screen);
  186.         if (XLookupColor (display, color_map,
  187.                 RED, &RGB_color, &hardware_color) != 0
  188.             &&
  189.             XAllocColor (display, color_map,
  190.                 &hardware_color) != 0)
  191.             background = hardware_color.pixel;
  192.         
  193.         if (XLookupColor (display, color_map,
  194.                 BLUE, &RGB_color, &hardware_color) != 0
  195.             &&
  196.             XAllocColor (display, color_map,
  197.                 &hardware_color) != 0)
  198.             foreground = hardware_color.pixel;
  199.     }    
  200. }   
  201.     
  202. main (argc, argv)    
  203.     int argc;    
  204.     char **argv;    
  205. {   XEvent event;    
  206.     
  207.     initX ();    
  208.     
  209.     main_window = openWindow (10, 20, 200, 100, 5, 
  210.                               argc, argv);    
  211.     gc = getGC ();    
  212.     
  213.     /* Display the window on the screen */    
  214.     XMapWindow (display, main_window);    
  215.     XFlush (display);    
  216.     while (True)    
  217.     {    
  218.           XNextEvent (display, &event);    
  219. #ifdef DEBUG    
  220.           printf ("Event number is %d\n", event.type);
  221. #endif    
  222.           switch  (event.type)    
  223.           {    
  224.           case Expose:  
  225.                       doExposeEvent (&event);    
  226.                       break;    
  227.     
  228.           case ButtonPress:    
  229.                       doButtonPressEvent (&event);    
  230.                       break;    
  231.     
  232.           case KeyPress:
  233.                       doKeyPressEvent (&event);    
  234.                       break;    
  235.     
  236.           case MappingNotify:    
  237.                       XRefreshKeyboardMapping (&event);
  238.                       break;    
  239.           }    
  240.     }    
  241. }
  242.